home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / stdio / tstscanf.c < prev    next >
C/C++ Source or Header  |  1992-03-17  |  3KB  |  101 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #ifdef    BSD
  21. #include </usr/include/stdio.h>
  22. #else
  23. #include <stdio.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28.  
  29. int
  30. DEFUN(main, (argc, argv), int argc AND char **argv)
  31. {
  32.   char buf[BUFSIZ];
  33.   FILE *in = stdin, *out = stdout;
  34.  
  35.   if (argc == 2 && !strcmp (argv[1], "-opipe"))
  36.     {
  37.       out = popen ("/bin/cat", "w");
  38.       if (out == NULL)
  39.     {
  40.       perror ("popen: /bin/cat");
  41.       exit (EXIT_FAILURE);
  42.     }
  43.     }
  44.   else if (argc == 3 && !strcmp (argv[1], "-ipipe"))
  45.     {
  46.       sprintf (buf, "/bin/cat %s", argv[2]);
  47.       in = popen (buf, "r");
  48.     }
  49.  
  50.   {
  51.     char name[50];
  52.     fprintf (out,
  53.          "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
  54.          sscanf ("thompson", "%s", name),
  55.          name);
  56.   }
  57.  
  58.   fputs ("Testing scanf (vfscanf)\n", out);
  59.  
  60.   fputs ("Test 1:\n", out);
  61.   {
  62.     int n, i;
  63.     float x;
  64.     char name[50];
  65.     n = fscanf (in, "%d%f%s", &i, &x, name);
  66.     fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
  67.          n, i, x, name);
  68.   }
  69.   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
  70.   fputs ("Test 2:\n", out);
  71.   {
  72.     int i;
  73.     float x;
  74.     char name[50];
  75.     (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
  76.     fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
  77.   }
  78.   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
  79.   fputs ("Test 3:\n", out);
  80.   {
  81.     float quant;
  82.     char units[21], item[21];
  83.     while (!feof (in) && !ferror (in))
  84.       {
  85.     int count;
  86.     quant = 0.0;
  87.     units[0] = item[0] = '\0';
  88.     count = fscanf (in, "%f%20s of %20s", &quant, units, item);
  89.     (void) fscanf (in, "%*[^\n]");
  90.     fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
  91.          count, quant, item, units);
  92.       }
  93.   }
  94.   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
  95.  
  96.   if (out != stdout)
  97.     pclose (out);
  98.  
  99.   exit(EXIT_SUCCESS);
  100. }
  101.